package com.yh.app.common.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.yh.app.common.SessionConfig;
import com.yh.base.util.CookieUtil;

/**
 * 
 * <功能简述>
 * 
 * @author  严华
 * @version  [版本号, 2012-11-4]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
public class SessionUtil {

    private final static Logger log = Logger.getLogger(SessionUtil.class);

    /**
     * 
     * 根据属性标识获取属性信息
     *
     * @author  严华
     * @param code
     * @param request
     * @return [参数说明]
     * 
     * @return Object [返回类型说明]
     * @exception throws [异常类型] [异常说明]
     */
    public static Object getSessionAttributeValue(String code, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(code);
        if (obj == null) {
            log.error("Session参数获取失败，重新初始化......SessionKey【" + code + "】");
            // 说明session 丢失or session 失效 or 跳转到另外台服务器去了，重新初始化一下session
            // 从cookie中获取user_id 即可
            CookieUtil cookie = new CookieUtil(request, ServletActionContext.getResponse());
            String user_id = cookie.getCookieValue("USER_ID");

            SessionUtil.init(user_id, request);
            obj = session.getAttribute(code);
        }

        return obj;
    }

    public static void init(String staffId, HttpServletRequest request) {
        SessionConfig config = new SessionConfig();
        config.init(staffId, request);
    }
    
    public static void destroySession() {
        SessionConfig config = new SessionConfig();
        config.destroySession();
    }
}
